package dao;

import java.util.ArrayList;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import readerRSS.RssItemController;
import readerRSS.RssItemModel;

/**
 *
 * @author Tania M
 */
public class RssItemModelDAO implements java.io.Serializable, IHibernate{
    
    private static ArrayList<RssItemModelDAO> list;

    /**
     * @return the list
     */
    public static ArrayList<RssItemModelDAO> getList() {
        return list;
    }

    /**
     * @param aList the list to set
     */
    public static void setList(ArrayList<RssItemModelDAO> aList) {
        list = aList;
    }
    
    private String title;
    private String link;
    private String description;
    private String category;
    private String guid;
    private RssChannelModelDAO belongsToChannel; //foreign key
    private ArrayList<RssItemModelDAO> plist;
    
    private int hasBeenSeen;
    
    /**
     * 
     */
    public RssItemModelDAO(){      
    }
    
    /**
     * 
     * @param obj
     */
    public RssItemModelDAO(RssItemModel obj){
        this.title=obj.getTitle();
        this.link=obj.getLink();
        this.description=obj.getDescription();
        this.guid=obj.getGuid();
        this.category=obj.getCategory();
    }
    
    /**
     * 
     * @param obj
     * @param object
     */
    public RssItemModelDAO(RssItemModel obj, RssItemController object){
        this(object.getModel()); //appelle l'autre constructeur
        this.hasBeenSeen=object.getHasBeenSeenState().getHasBeenSeen();
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * @return the link
     */
    public String getLink() {
        return link;
    }

    /**
     * @param link the link to set
     */
    public void setLink(String link) {
        this.link = link;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }


    

    /**
     * @return the category
     */
    public String getCategory() {
        return category;
    }

    /**
     * @param category the category to set
     */
    public void setCategory(String category) {
        this.category = category;
    }
    
    /**
     * 
     */
    @Override
    public void saveObject(){
        /*SessionFactory sessionFactory = new Configuration()
                .configure()
                .buildSessionFactory();*/
        
        SessionFactory sessionFactory = HibernateUtilFactory.getHibernateUtilRSS();
        
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        
        session.saveOrUpdate(this);
        
        session.getTransaction().commit();;
        session.close();
    }

    /**
     * 
     */
    public static void loadObjects() {
        SessionFactory sessionFactory = HibernateUtilFactory.getHibernateUtilRSS();
        
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        
        //On doit créer une query HQL pour ramener tous les éléments de la table
        //On utilise le langage integre, HQL
        Query query = session.createQuery("from RssItemModelDAO");
        setList((ArrayList<RssItemModelDAO>) (ArrayList)query.list());
        
        session.getTransaction().commit();;
        session.close();
    }

    /**
     * 
     */
    @Override
    public void create() {
        SessionFactory sessionFactory = HibernateUtilFactory.getHibernateUtilRSS();
        
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        
        session.save(this);
        
        session.getTransaction().commit();;
        session.close();
    }

    /**
     * 
     */
    @Override
    public void read() {
        SessionFactory sessionFactory = HibernateUtilFactory.getHibernateUtilRSS();
        
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        
        //On doit créer une query HQL pour ramener tous les éléments de la table
        //On utilise le langage integre, HQL
        //Query query = session.createQuery("from RssItemModelDAO where id="+id);
        Query query = session.createQuery("from RssItemModelDAO where id=?").setString(0, getGuid());
        plist = (ArrayList)query.list();
        
        /*
        //foreach de parcours de liste
        for(RssItemModelDAO elt : list){
            System.out.println(elt.getId());
        }
        //*/
        session.getTransaction().commit();
        session.close();
    }

    /**
     * 
     */
    @Override
    public void update() {
        SessionFactory sessionFactory = HibernateUtilFactory.getHibernateUtilRSS();
        
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        
        session.update(this);
        
        session.getTransaction().commit();;
        session.close();
    }

    /**
     * 
     */
    @Override
    public void delete() {
        SessionFactory sessionFactory = HibernateUtilFactory.getHibernateUtilRSS();
        
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        
        session.delete(this);
        
        session.getTransaction().commit();;
        session.close();
    }

    
    

    /**
     * @return the guid
     */
    public String getId() {
        return getGuid();
    }

    /**
     * @param guid the guid to set
     */
    public void setId(String guid) {
        this.setGuid(guid);
    }

    /**
     * @return the belongsToChannel
     */
    public RssChannelModelDAO getBelongsToChannel() {
        return belongsToChannel;
    }

    /**
     * @param belongsToChannel the belongsToChannel to set
     */
    public void setBelongsToChannel(RssChannelModelDAO belongsToChannel) {
        this.belongsToChannel = belongsToChannel;
    }

    /**
     * @return the guid
     */
    public String getGuid() {
        return guid;
    }

    /**
     * @param guid the guid to set
     */
    public void setGuid(String guid) {
        this.guid = guid;
    }

    /**
     * @return the hasBeenSeen
     */
    public int getHasBeenSeen() {
        return hasBeenSeen;
    }

    /**
     * @param hasBeenSeen the hasBeenSeen to set
     */
    public void setHasBeenSeen(int hasBeenSeen) {
        this.hasBeenSeen = hasBeenSeen;
    }

}
